import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class Game extends JPanel implements MouseListener, MouseMotionListener, Runnable
{
	private String m_message;
	/** where to draw the message */
	private Point m_textLocation = new Point();
	private Point m_lastMouseClick = new Point();
	
	Vector<Circle> circles = new Vector<Circle>();
	
	Circle m_selected = null;
	
	public static void main(String[] args)
	{
		JFrame jf = new JFrame("Graphs");
		Game g = new Game("hello!");
		jf.add(g);
		jf.setSize(640, 480);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setVisible(true);
	}
	public Game(String a_message)
	{
		m_message = a_message;
		addMouseListener(this);
		addMouseMotionListener(this);
		Circle A = new Circle("A", 20,20,10);
		Circle B = new Circle("B", 18,60,10);
		Circle C = new Circle("C", 40,70,10);
		Circle D = new Circle("D", 60,50,10);
		Circle E = new Circle("E", 50,16,10);
		A.addNeighbor(B);
		A.addNeighbor(C);
		C.addNeighbor(A);
		C.addNeighbor(E);
		E.addNeighbor(C);
		D.addNeighbor(E);
		D.addNeighbor(C);
		circles.add(A);
		circles.add(B);
		circles.add(C);
		circles.add(D);
		circles.add(E);
		Thread t = new Thread(this);
		t.start();
	}
	private int m_selectedAnimationState = 0;
	Color[] m_selectedColors = 
	{		new Color(0,192,255),
			new Color(0,128,255),
			new Color(0,0,255),
			new Color(0,128,255),};
	public void paintComponent(Graphics g)
	{
		// will clear the screen using the JPanel's default draw method
		super.paintComponent(g);
//		g.drawString(m_message, m_textLocation.x, m_textLocation.y);
		for(int i = 0; i < circles.size(); ++i)
		{
			circles.get(i).draw(g);
		}
		if(m_selected != null)
		{
			g.setColor(m_selectedColors[m_selectedAnimationState]);
			m_selected.draw(g);
		}
	}
	public void run()// TODO update method to make this thread the main game loop...
	{
		while(true)
		{
			try {Thread.sleep(100);} catch (InterruptedException e) {}
			if(m_selected != null)
			{
				if(++m_selectedAnimationState >= m_selectedColors.length)
					m_selectedAnimationState = 0;
				repaint();
			}
		}
	}
	
	/**
	 * @return the circle found at {@link #m_lastMouseClick}, or null if no circle was found
	 */
	Circle theNodeAt(Point p)
	{
		// select the node
		Circle found = null;
		for(int i = 0; found == null && i < circles.size(); ++i)
		{
			if(circles.get(i).contains(p))
			{
				found = circles.get(i);
			}
		}
		return found;
	}
	
	void addNeighborToSelectedNode()
	{
		if(m_selected != null)
		{
			Node next = theNodeAt(m_lastMouseClick);
			if(next != null)
			{
				if(!m_selected.isNeighborsWith(next))
				{
					m_selected.addNeighbor(next);
				}
				else
				{
					m_selected.removeNeighbor(next);
				}
			}
		}
	}
	
	void leftClickFunctionality(MouseEvent e)
	{
		if(e.isControlDown())
		{
			circles.add(new Circle("n"+circles.size(), m_lastMouseClick.x, m_lastMouseClick.y, 10));
		}
		else if(e.isAltDown())
		{
			addNeighborToSelectedNode();
		}
		else
		{
			m_selected = theNodeAt(m_lastMouseClick);
		}
	}
	
	public void rightClickFunctionality(MouseEvent e)
	{
		// and a node is selected
		if(m_selected != null)
			// change the label of that node
			m_selected.setLabel();
	}
	
// methods implemented for MouseListener
	public void mouseClicked(MouseEvent e){}
	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e){}
	public void mouseReleased(MouseEvent e){}
	public void mousePressed(MouseEvent e)
	{
		m_lastMouseClick.setLocation(e.getX(), e.getY());
		m_textLocation.setLocation(m_lastMouseClick);
		// if this is a non-right mouse click
		if(!e.isMetaDown())
		{
			// what if the user pressed the left mouse click?
			leftClickFunctionality(e);
		}
		// if this is a right mouse click
		else
		{
			rightClickFunctionality(e);
		}
		repaint();
	}
	public void mouseDragged(MouseEvent e)
	{
		m_textLocation.setLocation(e.getX(), e.getY());
		if(m_selected != null)
		{
			m_selected.setLocation(m_textLocation);
		}
		repaint();
	}
	public void mouseMoved(MouseEvent e){}

}
